//商品分类接口
var express = require('express');
var router = express.Router();
// 导入连接数据库key
const pool = require('../MySQLkey/kry');

/* GET categories listing. */
//获取categories表的所有数据
router.get('/', async function(req, res) {
  let connection;
  try {
    connection = await pool.getConnection();

    // 查询所有分类数据
    const query = 'SELECT * FROM categories ORDER BY sort_order ASC, id ASC';
    const [rows] = await connection.query(query);

    // 返回结果
    res.json({
      success: true,
      message: `获取到 ${rows.length} 个分类`,
      data: rows
    });

  } catch (err) {
    console.error('获取分类失败:', err);
    res.status(500).json({
      success: false,
      message: '获取分类失败',
      error: err.message
    });
  } finally {
    if (connection) connection.release();
  }
});

module.exports = router;
